{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aaca0939",
   "metadata": {},
   "outputs": [],
   "source": [
    "import findspark\n",
    "import jieba\n",
    "from pyspark import SparkContext, SparkConf\n",
    "\n",
    "findspark.init()\n",
    "conf = SparkConf()\n",
    "sc = SparkContext(conf=conf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7a5bce51",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 根据用户搜索日志统计每个小时内查询关键词词频的TopNc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "25532071",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "95618\n"
     ]
    }
   ],
   "source": [
    "#读取日志，并缓存\n",
    "path = \"file:///C:/Users/Administrator/Desktop/demo/search.log\"\n",
    "rdd = sc.textFile(path)\n",
    "#缓存\n",
    "rdd.cache()\n",
    "\n",
    "print(rdd.count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "526e853f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "698\n"
     ]
    }
   ],
   "source": [
    "#构建停用词字典\n",
    "stopWords = {}\n",
    "lines = open(\"C:/Users/Administrator/Desktop/demo/stopwords.txt\",\"r\",encoding=\"utf-8\").read()\n",
    "for v in lines:\n",
    "    stopWords[v]=1\n",
    "\n",
    "brStopWords = sc.broadcast(stopWords)\n",
    "print(len(brStopWords.value))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "11346428",
   "metadata": {},
   "outputs": [],
   "source": [
    "#提取关键词\n",
    "def extractFun(w):\n",
    "    t = w.split(\"\\t\")\n",
    "    hour = t[0][:2]\n",
    "    keys = list(jieba.cut_for_search(t[2][1:-1]))\n",
    "    #url_key\n",
    "    \n",
    "    return [ hour+\"_\"+k for k in keys if brStopWords.value.get(k,None)==None ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e09a9d77",
   "metadata": {},
   "outputs": [],
   "source": [
    "#转换结构\n",
    "def trans1(w):\n",
    "    t = w[0].split(\"_\")\n",
    "    \n",
    "    return (t[0], (t[1],w[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "042d963b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#排序\n",
    "def topN(x):\n",
    "    #组内排序\n",
    "    result = sorted(list(x[1]), key=lambda x:x[1], reverse=True)\n",
    "    return (x[0], result[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0ec439ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('19', [('地震', 5168), ('救灾', 4565), ('物资', 4364), ('哄抢', 4363), ('救灾物资', 4363)])]\n"
     ]
    }
   ],
   "source": [
    "#统计小时——关键词 词频\n",
    "rdd1 = rdd \\\n",
    "    .flatMap(extractFun) \\\n",
    "    .map(lambda x:(x,1)) \\\n",
    "    .reduceByKey(lambda x,y:x+y) \\\n",
    "    .filter(lambda x:x[1]>1) \\\n",
    "    .map(trans1) \\\n",
    "    .groupByKey() \\\n",
    "    .map(topN)\n",
    "\n",
    "print(rdd1.collect())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c6c884c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "e42634819b8c191a5d07eaf23810ff32516dd8d3875f28ec3e488928fbd3c187"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
